package com.cloud.wyf.Controller;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.cloud.wyf.Entity.*;
import com.cloud.wyf.Entity.Dto.DataDto;
import com.cloud.wyf.Service.VoteService;
import com.cloud.wyf.Util.GetTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/votedata")
public class DataController {

    @Autowired
    VoteService voteService;

    @GetMapping
    public String getData(HttpServletRequest request){
        String s= GetTokenUtil.getInfoFromToken(request.getHeader("x-access-token"),"role");
        int role = Integer.valueOf(s);
        if(role!=0) return JSONObject.toJSONString(Message.failure("You have no power"));
        List<Data> data = voteService.queryData(request.getHeader("x-access-token"));
        Map<String,Integer> map=new HashMap<>(data.size());
        Map<String,Integer> map1=new HashMap<>(data.size());
        List<DataDto> result=new ArrayList<>(data.size());
        //TODO 判断中层干部是否被投票和普通职工是否投票
        List<Middel> allMiddel = voteService.queryAllMiddel(request.getHeader("x-access-token"));
        List<Ordinary> ordinaries = voteService.queryAllOrdinary(request.getHeader("x-access-token"));
            for(Middel middel:allMiddel){
                for(Data d: data){
                    if(d.getIncludePart().contains(middel.getPart())){
                        List<LeaderScore> leaderScores = voteService.selectLeaderScoreByOid(middel.getUid());
                        if(leaderScores.size()==0&&map.get(d.getDirectparty())==null) {
                            map.put(d.getDirectparty(), 0);
                            continue;
                        }
                        for(LeaderScore leaderScore:leaderScores) {
                            if(leaderScore.getStatus()==1) {
                                map.put(d.getDirectparty(), map.getOrDefault(d.getDirectparty(), 0) + 1);
                                break;
                            }
                        }
                    }
                }
            }
        for(Ordinary ordinary:ordinaries){
            for(Data d: data){
                if(d.getIncludePart().contains(ordinary.getPart())){
                    List<OrdinaryScore> ordinaryScores = voteService.selectOrdinaryScoreByOid(ordinary.getUid());
                    if(ordinaryScores.size()==0&&map1.get(d.getDirectparty())==null) {
                        map1.put(d.getDirectparty(), 0);
                        continue;
                    }
                    for(OrdinaryScore ordinaryScore:ordinaryScores) {
                        if(ordinaryScore.getStatus()==1) {
                            map1.put(d.getDirectparty(), map1.getOrDefault(d.getDirectparty(), 0) + 1);
                            break;
                        }
                    }
                }
            }
        }
        for(Data d:data){
            Float mrate=new Float(0);
            Float orate=new Float(0);
            if(map.get(d.getDirectparty())!=0&&map1.get(d.getDirectparty())!=0){
                mrate=(float) map.get(d.getDirectparty())/(float) d.getMiddelnum();
                orate=(float)map1.get(d.getDirectparty())/(float)d.getOrdinarynum();
            }else if(map.get(d.getDirectparty())!=0&&map1.get(d.getDirectparty())==0){
                mrate=(float) map.get(d.getDirectparty())/(float) d.getMiddelnum();
                orate=new Float(0);
            }else if(map.get(d.getDirectparty())==0&&map1.get(d.getDirectparty())!=0){
                mrate=new Float(0);
                orate=(float)map1.get(d.getDirectparty())/(float)d.getOrdinarynum();
            }else {
                mrate=new Float(0);
                orate=new Float(0);
            }
            result.add(new DataDto(d.getDirectparty(),d.getMiddelnum(),map.get(d.getDirectparty()),mrate,d.getOrdinarynum(),map1.get(d.getDirectparty()),orate));
        }
        return JSONObject.toJSONString(Message.success(result));
    }
}
